package zct.proj.equipModel;

import java.util.HashMap;

import zct.proj.pojo.EquipmentInfo;
import zct.proj.util.Const;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Paint.Align;

public class DHANModel extends BaseModel {

	private int radius;
//	private HashMap<String, Point> lampCenter = new HashMap<String, Point>();
//	private HashMap<String, Path> arrowMap = new HashMap<String, Path>();
	private HashMap<String, DHANInfo> infoMap = new HashMap<String, DHANInfo>();
	
	public int getRadius() {
		return radius;
	}
	
	public Point getRingPos(int type, int dir, int width) {
		
//		String key = type + "_" + dir + "_" + width;
//		Point p = lampCenter.get(key);
		
//		if (p == null) {
		Point p = new Point();
		int flg = 1;
		if (dir %2 == 1) {
			flg = -1;
		}
		
		switch (type) {
		case Const.TYPE_BTN_LCAN:
			// 灯或按钮 列车按钮
			p.set(xSpacing / 2, ySpacing * (dir % 2) + ySpacing);
			break;
		case Const.TYPE_BTN_DCBS:
			// 灯或按钮 道岔表示
		case Const.TYPE_BTN_DCAN:
			// 灯或按钮 调车按钮
		case Const.TYPE_BTN_DQ:
			// 灯或按钮 单圈
		case Const.TYPE_BTN_SQ:
			// 灯或按钮 双圈
			p.set(xSpacing * width / 2, ySpacing / 2 * flg);
			break;
		}
//			lampCenter.put(key, p);
//		}
		return p;
	}
	
	/**
	 * 绘制箭头
	 * @param start 起始点
	 * @param width rect宽
	 * @param height rect高
	 * @param dir 方向（偶→奇←）
	 * @return
	 */
	public Path getArrowPath(int width, int height, int dir) {
//		String key = "f" + width + "_" + height + "_" + dir;
//		Path path = arrowMap.get(key);
//		
//		if (path == null) {
		int startX = (int) (0.095f * width * xSpacing);
		int startY = (int) (0.29f * height * ySpacing);
		float tmpX = 0;
		float tmpY = 0;
		int flg = 1;
		if (dir % 2 == 1) {
			flg = -1;
		}
		Path path = new Path();
		// 起始点，左上
		path.moveTo(0, 0);
		tmpX = 0.6f * xSpacing * width * flg;
		// 箭头尾巴上横线
		path.lineTo(tmpX, 0);
		// 箭头上竖线
		tmpY = -0.21f * ySpacing * height;
		path.lineTo(tmpX, tmpY);
		// 箭头上斜线
		tmpY = -tmpY;
		path.lineTo(0.81f * xSpacing * width * flg, tmpY);
		// 箭头下斜线
		path.lineTo(tmpX, tmpY * 3);
		// 箭头下竖线
		path.lineTo(tmpX, tmpY * 2);
		// 尾部下横线
		path.lineTo(0, tmpY * 2);
		path.close();
		if (flg > 0) {
			path.offset(startX, startY);
		} else {
			path.offset(width * xSpacing - startX, startY);
		}
//			arrowMap.put(key, path);
//		}
		return path;
	}
	
	/**
	 * 绘制半箭头
	 * @param start 起始点
	 * @param width rect宽
	 * @param height rect高
	 * @param dir 方向（偶→奇←）
	 * @return
	 */
	public Path getHalfArrowPath(int width, int height, int dir) {
//		String key = "h" + width + "_" + height + "_" + dir;
//		Path path = arrowMap.get(key);
//		
//		if (path == null) {
		int startX = (int) (width * xSpacing * 0.095);
		int startY = (int) (height * ySpacing * 0.29);
		float tmpX = 0;
		float tmpY = 0;
		int flg = 1;
		if (dir % 2 == 1) {
			flg = -1;
		}
		Path path = new Path();
		// 起始点，左上
		path.moveTo(0, 0);
		tmpX = (float) (xSpacing * width * 0.5 * flg);
		// 箭头尾巴上横线
		path.lineTo(tmpX, 0);
		// 箭头上竖线
		tmpY = (float) (-ySpacing * height * 0.21);
		path.lineTo(tmpX, tmpY);
		// 箭头上斜线
		tmpY = -tmpY * 2;
		path.lineTo((float) (xSpacing * width * 0.81 * flg), tmpY);
		// 下横线
		path.lineTo(0, tmpY);
		path.close();
		if (flg > 0) {
			path.offset(startX, startY);
		} else {
			path.offset(width * xSpacing - startX, startY);
		}
//			arrowMap.put(key, path);
//		}
		return path;
	}
	
	public DHANModel(float scale, int cellWidth, int cellHeight) {
		super(scale, cellWidth, cellHeight);
	}

	@Override
	protected void init() {
		super.init();
		radius = (int) (0.3 * xSpacing);
	}

	public DHANInfo getDHANInfo(EquipmentInfo sb) {
		
		String key = sb.getType() + "_" + sb.getDir() + "_" + sb.getWidth() + "_" + sb.getHeight();
		
		DHANInfo dInfo = infoMap.get(key);
		if (dInfo != null) {
			return dInfo;
		}
		
		if (sb.getType() == Const.TYPE_BTN_LCAN
				|| sb.getType() == Const.TYPE_BTN_DCBS
				|| sb.getType() == Const.TYPE_BTN_DCAN
				|| sb.getType() == Const.TYPE_BTN_DQ
				|| sb.getType() == Const.TYPE_BTN_SQ) {
			// 灯或按钮 列车按钮  道岔表示  调车按钮  单圈   双圈
			Point p = getRingPos(sb.getType(), sb.getDir(), sb.getWidth());
			dInfo = new DHANInfo(sb.getType(), p, radius);
			Point pt = new Point(p.x, p.y + ySpacing + radius);
			if (sb.getDisp() != null) {
				dInfo.setFont(pt, Align.CENTER);
			}
		} else if (sb.getType() == Const.TYPE_BTN_FX) {
			// 灯或按钮 方形
			Point p = new Point((int) (sb.getWidth() / 2), 0);
			dInfo = new DHANInfo(sb.getType(), p, (int) 0.8f * xSpacing);
			Point pt = new Point(sb.getWidth() * xSpacing / 2, ySpacing);
			if (sb.getDisp() != null) {
				dInfo.setFont(pt, Align.CENTER);
			}
		} else if (sb.getType() == Const.TYPE_BTN_ARROW) {
			// 灯或按钮 全箭头
			Path arrow = getArrowPath(sb.getWidth(), sb.getHeight(), sb.getDir());
			dInfo = new DHANInfo(sb.getType(), arrow);
		} else if (sb.getType() == Const.TYPE_BTN_HALFARROR) {
			// 灯或按钮 半箭头
			Path arrow = getHalfArrowPath(sb.getWidth(), sb.getHeight(), sb.getDir());
			dInfo = new DHANInfo(sb.getType(), arrow);
		}
		infoMap.put(key, dInfo);
		return dInfo;
	}
}
